package com.example.utils;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

import Adapter.ViewPageFragmentAdapter;

public class ViewPagerAndFragment extends AppCompatActivity {

    List<inneViewPagerFragment> list = new ArrayList<>();
    List<String> message = new ArrayList<>();
    ViewPager2 viewPager2;
    TabLayout tabLayout;
    Integer[] icon = {R.drawable.blueberry,R.drawable.cherry,R.drawable.pineapple,R.drawable.strawberry};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager_and_fragment);

        initViewPager();



    }


    public void initViewPager(){
        Bundle bundle = new Bundle();
        bundle.putString("message","This is Test about Bundle ");
        inneViewPagerFragment fragment = new inneViewPagerFragment();
        fragment.setArguments(bundle);


        message.add("blueberry");
        message.add("cherry");
        message.add("pineapple");
        message.add("strawberry");

        viewPager2 = findViewById(R.id.ViewPager2);
        tabLayout = findViewById(R.id.tabMode);
        for (int i = 0; i < 4; i++) {
            list.add(inneViewPagerFragment.newInstance(message.get(i)));
        }
        ViewPageFragmentAdapter adapter = new ViewPageFragmentAdapter(getSupportFragmentManager(),getLifecycle(),list);
        viewPager2.setAdapter(adapter);

        //这里必须要在添加适配器完了之后才能新建该方法,设置ViewPager2与TabLayout联动
        new TabLayoutMediator(tabLayout, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(message.get(position));
                tab.setIcon(icon[position]);
            }
        }).attach();

        //设置Tab的点击事件
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @SuppressLint("ResourceAsColor")
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                TextView textView = new TextView(ViewPagerAndFragment.this);
                textView.setTextSize(16);
                textView.setTextColor(R.color.purple_200);
                textView.setText(tab.getText());
                tab.setCustomView(textView);

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                //这个必须得设置，不然会一直叠加
                tab.setCustomView(null);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }
}